﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using MoonLight.LibCommon;

namespace MoonLight.LibCommon
{
    public class ConfigOperator
    {
        public int GetStrValue(string section, string key, ref string value)
        {
            int result = 0;
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            try
            {
                switch (section)
                {
                    case "appSettings":
                        AppSettingsSection appSection = (AppSettingsSection)config.GetSection("appSettings");
                        value = appSection.Settings[key].Value;
                        break;
                    default:
                        Logger.WriteLog("unknown section[" + section + "]");
                        result = -1;
                        break;
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLog("get str value [" + section + "][" + key + "] fail[" + ex.Message + "]");
                result = -1;
            }
            return result;
        }

        public int GetIntValue(string section, string key, ref int value)
        {
            int result = 0;
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            try
            {
                switch (section)
                {
                    case "appSettings":
                        AppSettingsSection appSection = (AppSettingsSection)config.GetSection("appSettings");
                        value = int.Parse(appSection.Settings[key].Value);
                        break;
                    default:
                        Logger.WriteLog("unknown section[" + section + "]");
                        result = -1;
                        break;
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLog("get str value [" + section + "][" + key + "] fail[" + ex.Message + "]");
                result = -1;
            }
            return result;
        }
    }
}
